#!/bin/bash

# If the execution result of any statement is not true, should exit
set -e

# get current path
BASEPATH=$(cd "$(dirname $0)"; pwd)

# build path
export BUILD_PATH="${BASEPATH}/build"

# output path
export OUTPUT_PATH="${BASEPATH}/output"

_COUNT=0

define_opt() {
    local var=$1
    local default=$2
    local opt=$3
    local flag=$4
    local intro=$5

    # count opt number
    _COUNT=$((${_COUNT} + 1))

    local max_flag=8
    local mod_max=$((${_COUNT} % ${max_flag}))
    local flag_space="              "
    local intro_space="     "

    # set global varible
    if [[ "X${var}" != "X" ]] && [[ "X${default}" != "X" ]]; then
        eval "${var}=${default}"
    fi
    _OPTS="${_OPTS}${opt}"
    if [[ "X${mod_max}" != "X1" ]] && [[ "X${mod_max}" != "X${_COUNT}" ]]; then
        _FLAGS="${_FLAGS}\n${flag_space}[${flag}]"
    else
        _FLAGS="${_FLAGS} [${flag}]"
    fi
    _INTROS="${_INTROS}${intro_space}${flag} ${intro}\n"
}

# define option
define_opt ""                ""                   "h"  "-h"                     "Print usage"
define_opt DEBUG_MODE        "off"                "d"  "-d"                     "Enable debug mode, default off"
define_opt TEST_MODE         "off"                "t"  "-t"                     "Enable building ut, st and other test binaries, default off"
define_opt VERBOSE_MODE      "off"                "v"  "-v"                     "Enable building verbose mode, defalut off"
define_opt UPDATE_SUBMODULE  "off"                "u"  "-u"                     "Update submodule, defalut off"
define_opt THREAD_NUM        16                    "j:" "-j[n]"                  "Set the threads number when building, default 16"

# print usage message
usage()
{
    printf "Usage:\nbash build.sh${_FLAGS}\n\n"
    printf "Options:\n${_INTROS}"
}

# check and set options
checkopts()
{
    # process the options
    while getopts "${_OPTS}" opt
    do
    OPTARG=$(echo ${OPTARG} | tr '[A-Z]' '[a-z]')
    case "${opt}" in
        v)
            VERBOSE_MODE="on"
            ;;
        d)
            DEBUG_MODE="on"
            ;;
        j)
            THREAD_NUM=${OPTARG}
            ;;
        h)
            usage
            exit 0
            ;;
        t)
            ENABLE_TESTS="on"
            ;;
        u)
            UPDATE_SUBMODULE="on"
            ;;
        *)
            echo "Unknown option ${opt}!"
            usage
            exit 1
    esac
    done
}

checkopts "$@"

echo "---------------- CHelper: build start ----------------"

build_chelper()
{
    # create build folder if not exists
    mkdir -pv ${BUILD_PATH}
    mkdir -pv ${OUTPUT_PATH}

    # entry build
    cd ${BUILD_PATH}

    # CMAKE_XXX_FLAGS
    CMAKE_ARGS="-DWITH_CUSTOM_PREFIX=ON -DDEBUG_MODE=$DEBUG_MODE"
    if [[ "X${DEBUG_MODE}" = "Xoff" ]]; then
        echo "building release version"
        CMAKE_ARGS="${CMAKE_ARGS} -DCMAKE_BUILD_TYPE=Release"
    else
        echo "building debug version"
        CMAKE_ARGS="${CMAKE_ARGS} -DCMAKE_BUILD_TYPE=Debug"
    fi

    if [[ "X${VERBOSE_MODE}" = "Xon" ]]; then
        CMAKE_VERBOSE="--verbose"
    fi

    if [[ "X${ENABLE_TESTS}" = "Xon" ]]; then
        CMAKE_ARGS="${CMAKE_ARGS} -DENABLE_TESTCASES=ON"
    else
        rm -f ${OUTPUT_PATH}/bin/chelper_ut
    fi

    if [[ "X${UPDATE_SUBMODULE}" == "Xon" ]]; then
        # update submodule
        git submodule update --init --recursive
    fi

    # first cmake
    cmake $CMAKE_ARGS ..
    # second build
    cmake --build ./ ${CMAKE_VERBOSE} -j$THREAD_NUM

    # check state
    if [ $? -ne 0 ]; then
        echo "Failed to build CHelper."
        exit 1
    fi
}

build_chelper